package com.changgou.business.listener;

import com.thoughtworks.xstream.io.xml.xppdom.Xpp3DomBuilder;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Map;

@Component
public class AdListener {
    @Autowired
    private RestTemplate restTemplate;

    //1.从rabbitmq中接收广告信息改变
    //2 http://192.168.200.128/ad_update?position=web_index_lb  Nginx  mysql->redis
    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(String message) {

        System.out.println("接收到的消息是:" + message);

        //发起远程调用
        String url = "http://192.168.200.128/ad_update?position=" + message;
        ResponseEntity<Map> forEntity = restTemplate.getForEntity(url,Map.class);
        Map body = forEntity.getBody();
        System.out.println(body);



        //okHttp的方法
        /*
        //发起远程调用
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "http://192.168.200.128/ad_update?position=" + message;
        Request request = new Request.Builder().url(url).build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            //失败时要做的事儿
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
            //成功时要做的事儿
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功:"+response.message());
            }
        });
        */
    }
}
